﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;


namespace weikebuluo.web.admin.user
{
    public partial class user : weikeweixin.web.admin.cs.userbase
    {


        a_user_dal userdal = new a_user_dal();
        protected a_user_model usermodel = new a_user_model();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["user_id"]))
            {

                usermodel = userdal.GetModel(int.Parse(Request.QueryString["user_id"]));
            }
            switch (Request.Form["action"])
            {
                case "upd":
                    updateUser();
                    break;

            }

        }


        private void updateUser()
        {
            if (!string.IsNullOrEmpty(Request.Form["user_id"]))
            {
                usermodel = userdal.GetModel(int.Parse(Request.Form["user_id"]));
            }

            string User_Name = Request.Form["username"].Trim();
            string User_Phone = Request.Form["phone"].Trim();
            string User_Email = Request.Form["email"].Trim();
            string User_Pwd = Request.Form["password"].Trim();
            string User_QQ = Request.Form["qq"].Trim();
            if (!PageValidate.IsValidInput(ref User_Name, true))
            {
                Response.Write("请输入正确的用户名");
            }

            //else if (string.IsNullOrEmpty(User_Pwd))
            //{
            //    Response.Write("请输入正确的密码");
            //}

            else if (!PageValidate.IsNumber(User_Phone) || User_Phone.Length != 11)
            {
                Response.Write("请输入正确的手机号码");
            }
            else if (!PageValidate.IsEmail(User_Email))
            {
                Response.Write("请输入正确的邮箱");
            }
            else
            {
                // usermodel.user_addtime = DateTime.Now;
                usermodel.user_email = User_Email;
                usermodel.user_name = User_Name;
                usermodel.user_phone = User_Phone;

                if (string.IsNullOrEmpty(User_Pwd))
                {
                }
                else
                {
                    if (MD5Provider.GetMd5(User_Pwd) != usermodel.user_psw)
                    {
                        usermodel.user_psw = MD5Provider.GetMd5(User_Pwd);
                    }
                }


                usermodel.user_qq = User_QQ;
                usermodel.user_lasttime = DateTime.Now;
                if (string.IsNullOrEmpty(Request.Form["user_id"]))
                {
                    userdal.Add(usermodel);
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"userlist.aspx\",\"error\":\"\"}");
                }
                else
                {
                    bool re = userdal.Update(usermodel);
                    if (re)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"userlist.aspx\",\"error\":\"\"}");

                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"失败！\",\"url\":\"userlist.aspx\",\"error\":\"\"}");
                    }
                }
            }
            Response.End();
        }
    }
}